/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

import com.bayu.common.lib.CommonUtil;
import com.bayu.services.dao.AverageMouldingDao;
import com.bayu.services.entity.AverageMoulding;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Bayu
 */
@Controller
@RequestMapping("/tpsaverage-input.htm")
public class TPSAverageInput{
    AverageMouldingDao dao;

    @Autowired
    private void setTStockDao(AverageMouldingDao dao){
      this.dao = dao;
    }

    @RequestMapping(method = RequestMethod.GET)
    public void displayForm(ModelMap map,String id,String tpstype){
	map.addAttribute("tpstype",tpstype);
        map.addAttribute("minYear", CommonUtil.getCurrentYear());
        map.addAttribute("maxYear", CommonUtil.getCurrentYear()+3);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String proccessForm(@ModelAttribute("AverageMoulding") AverageMoulding averageMoulding , BindingResult result,ModelMap map, String date,HttpServletRequest req){
            String type = req.getParameter("tpstype");
            String strReturn ="";
            DateFormat formarter = new SimpleDateFormat("yy-MM-dd");
            Date tanggal = null;
            String month = req.getParameter("month");
            String year = req.getParameter("year");
            date = year+"-"+month+"-"+"01";

            try {
                tanggal = formarter.parse(date);
            } catch (ParseException ex) {
                Logger.getLogger(AvgOdcInput.class.getName()).log(Level.SEVERE, null, ex);
            }

            averageMoulding.setDate(tanggal);
            
            List total = this.dao.custom("select count(*) from AverageMoulding where type='"+type+"' and date='"+date+"'");

            String strTotal=total.get(0).toString();


            int c = Integer.valueOf(strTotal).intValue();
            System.out.println(strTotal+"---"+c+"--"+date);
            if(c > 0){
                return "redirect:ErrorDuplicate.html";
            }else{
               
                 this.dao.save(averageMoulding);
                 if(type.equals("linemoulding")){
                    strReturn= "redirect:view-average-moulding-data.htm?msg=Data has been updated";
                 }
                 if(type.equals("tapping")){
                    strReturn= "redirect:view-average-tapping-data.htm?msg=Data has been updated";
                 }
                 if(type.equals("cnc")){
                    strReturn= "redirect:view-average-cnc-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mccomp")){
                    strReturn= "redirect:view-average-machining-comp-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mcp1")){
                    strReturn= "redirect:view-average-machining-p1-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("operation-mc")){
                    strReturn= "redirect:view-average-operation-machining-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mouldfoundry")){
                    strReturn= "redirect:view-average-mould-foundry-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mouldpms")){
                    strReturn= "redirect:view-average-mould-pms-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mouldsm60")){
                    strReturn= "redirect:view-average-mould-sm60-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("moulddisa")){
                    strReturn= "redirect:view-average-mould-disa-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mould2ml")){
                    strReturn= "redirect:view-average-mould-2ml-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("shaft2w")){
                    strReturn= "redirect:view-average-mould-shaft2w-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("shaft4w")){
                    strReturn= "redirect:view-average-mould-shaft4w-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("machining1")){
                    strReturn= "redirect:view-tps-average-machining1-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("machining2")){
                    strReturn= "redirect:view-tps-average-machining2-data.htm?msg=Data has been updated";
                 }

                 return strReturn;

            }
    }
}

